/**
 *Strings.java
 *@anthor Daniel Liu
 *2006-5-7 ����05:37:52
 **/
package org.lex.utils;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Iterator;

public class ToString {
	private static final String defaultPrefix = "[";
	private static final String defaultPostfix = "]";
	private static final String defaultSeparator = ", ";

	/***************************************************************************
	 * Throwable toString
	 **************************************************************************/
	public static String toString(Throwable t) {
		StringWriter w = new StringWriter();
		t.printStackTrace(new PrintWriter(w));
		return w.getBuffer().toString();
	}

	/***************************************************************************
	 * Array And Collection to String
	 **************************************************************************/
	public static <T> String toString(final T[] array) {
		return toString(array, defaultPrefix, defaultPostfix, defaultSeparator);
	}

	public static <T> String toString(final T[] array, String separator) {
		return toString(array, defaultPrefix, defaultPostfix, separator);
	}

	public static <T> String toString(final T[] array, String prefix, String postfix, String separator) {
		return toString(new Iterable<T>() {
			@Override
			public Iterator<T> iterator() {
				return new Iterator<T>() {
					private int index = 0;

					@Override
					public boolean hasNext() {
						return index < array.length;
					}

					@Override
					public T next() {
						T value = array[index];
						index++;
						return value;
					}

					@Override
					public void remove() {
					}
				};
			}
		}, prefix, postfix, separator);
	}

	/**
	 * ����Iterable�����toString����
	 */
	public static <T> String toString(Iterable<T> value) {
		return toString(value, defaultPrefix, defaultPostfix, defaultSeparator);
	}

	public static <T> String toString(Iterable<T> value, String separator) {
		return toString(value, defaultPrefix, defaultPostfix, separator);
	}

	public static <T> String toString(Iterable<T> value, String prefix, String postfix, String separator) {
		if (value == null)
			return "";
		Iterator<T> itor = value.iterator();
		if (false == itor.hasNext())
			return "";
		if (null == prefix)
			prefix = defaultPrefix;
		if (null == postfix)
			postfix = defaultPostfix;
		if (null == separator)
			separator = defaultSeparator;
		StringBuilder buffer = new StringBuilder(prefix);
		while (true) {
			T next = itor.next();
			if (next == value)
				buffer.append("(this collection)");
			else
				buffer.append(String.valueOf(next));
			if (itor.hasNext())
				buffer.append(separator);
			else
				break;
		}
		buffer.append(postfix);
		return buffer.toString();
	}

	public static <T> StringBuilder format(Iterator<T> iter, StringBuilder buf, String prefix, String postfix, String separator) {
		// check null constants
		if (null == prefix)
			prefix = defaultPrefix;
		if (null == postfix)
			postfix = defaultPostfix;
		if (null == separator)
			separator = defaultSeparator;
		// check null buf
		if (null == buf)
			buf = new StringBuilder();
		buf.append(prefix);
		// check null iter
		if (null == iter || false == iter.hasNext())
			return buf.append(postfix);
		while (true) {
			T next = iter.next();
			if (next == iter)
				buf.append("(this collection)");
			else
				buf.append(String.valueOf(next));
			if (iter.hasNext())
				buf.append(separator);
			else
				break;
		}
		buf.append(postfix);
		return buf;
	}

	private ToString() {
	}
}
